﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Admin.Permission.Role {
    public partial class Edit : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        String RoleKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            RoleKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                if (!String.IsNullOrEmpty(RoleKey)) {
                    FillForm(RoleKey);
                }
            }
        }

        void FillForm(String rRoleKey) {
            TSysRoleEO eo = biz.Load<TSysRoleEO>(rRoleKey);
            this.txt_RoleName.Text = eo.RoleName;
            this.txt_RoleLevel.Text = Convert.ToString(eo.RoleLevel);
            this.cbx_IsSysop.Checked = (eo.IsSysop==1);
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_RoleName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }

            if (String.IsNullOrEmpty(RoleKey)) {
                TSysRoleEO eo = new TSysRoleEO();
                eo.RoleName = this.txt_RoleName.Text;
                eo.IsSysop = (this.cbx_IsSysop.Checked) ? 1 : 0;
                eo.OwnerKey = this.OwnerKey;
                try {
                    eo.RoleLevel = Convert.ToInt32(this.txt_RoleLevel.Text);
                } catch { }
                if (eo.RoleLevel > BizConst.MAX_LEVEL) {
                    SiteInfo.Send(Page, "", "角色等级值太高，最大不能超过" + BizConst.MAX_LEVEL);
                    return;
                }
                RoleKey = (String)biz.Save<TSysRoleEO>(eo);
            } else {
                TSysRoleEO eo = biz.Load<TSysRoleEO>(RoleKey);
                eo.RoleName = this.txt_RoleName.Text;
                eo.IsSysop = (this.cbx_IsSysop.Checked) ? 1 : 0;
                try {
                    eo.RoleLevel = Convert.ToInt32(this.txt_RoleLevel.Text);
                } catch { }
                if (eo.RoleLevel > BizConst.MAX_LEVEL) {
                    SiteInfo.Send(Page, "", "角色等级值太高，最大不能超过" + BizConst.MAX_LEVEL);
                    return;
                } 
                biz.Update<TSysRoleEO>(eo);
            }
            Response.Redirect("List.aspx?id="+RoleKey);
        }
    }
}
